package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.product;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.net.URLEncoder;

import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.product.PdtAuthBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.product.PdtClassBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.product.ProductBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.product.SpecialOfferBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.GetPriceRecordExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.PdtAuthExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.PdtClassExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.PdtInfExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ISysUserPlantConnService;
import com.xiaoxu.intranetweb.util.PermissionCheck;
import com.xiaoxu.intranetweb.util.SzResult;
import com.xiaoxu.intranetweb.util.WebFunctionUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * 品种查询控制器
 *
 * @author xiaoxu
 * @version 1.0
 * @date 2022/1/5 15:00
 * @since 1.0
 */
@CrossOrigin()
@RestController
@RequestMapping(value = "/product")
public class ProductController {
    @Autowired
    private ProductBu productBu;
    @Autowired
    private PdtClassBu pdtClassBu;
    @Autowired
    private PdtAuthBu pdtAuthBu;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private ISysUserPlantConnService userPlantConnService;
    @Autowired
    private SpecialOfferBu specialOfferBu;
    @Autowired
    private WebFunctionUtil webFunctionUtil;

    /**
     * 所有品种查询接口
     *
     * @param value:品种名称
     * @return SzResult
     */
    @GetMapping("/getProduct")
    public SzResult getAllProduct(@RequestParam(value = "value", required = false) String value,
                                  @RequestParam(value = "pdtClass", required = false) String pdtClass,
                                  @RequestParam(value = "stockCls", required = false) Integer stockCls,
                                  HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return productBu.getProduct(value, pdtClass, stockCls, token, plantId);
    }
    //查询石膏渣品种产品
    @GetMapping("/getSaleProduct")
    public SzResult getSaleProduct(HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return productBu.getSaleProduct(plantId);
    }
    @PostMapping(value = "/getGypsumPlanPdts")
    @ApiOperation(value = "获取石膏计划产品", httpMethod = "POST", notes = "获取石膏计划产品")
    public SzResult getGypsumPlanPdts(@RequestBody JSONObject jsonObject,@RequestHeader("Authorization") String token,@RequestHeader("Plant-Id") String plantId) {
        String pdtName = jsonObject.getString("pdtName");
        return productBu.getGypsumPlanPdts(pdtName,plantId);
    }
    /**
     * 品种类型查询接口
     *
     * @return SzResult
     */
    @GetMapping("/getProductClass")
    public SzResult getProductByPdtNo(HttpServletRequest request) {
        String plantId = request.getHeader("Plant-Id");
        return pdtClassBu.getProductClass(plantId);
    }

    //新增品种类型
    @PostMapping("/addProductClass")
    @PermissionCheck("product_addProductClass")
    public SzResult addProductClass(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String pdtClass = jsonObject.getString("pdtClass");//品种类型
        String remark = jsonObject.getString("remark");//备注
        String srvTmsTamp = jsonObject.getString("srvTmsTamp");//服务器时间
        String cliTmsTamp = jsonObject.getString("cliTmsTamp");//客户端时间
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return pdtClassBu.addProductClass(pdtClass, remark, srvTmsTamp, cliTmsTamp, token, plantId);
    }

    //修改品种类型
    @PostMapping("/updateProductClass")
    @PermissionCheck("product_updateProductClass")
    public SzResult updateProductClass(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String id = jsonObject.getString("id");//品种类型id
        String pdtClass = jsonObject.getString("pdtClass");//品种类型
        String remark = jsonObject.getString("remark");//备注
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return pdtClassBu.updateProductClass(id, pdtClass, remark, token, plantId);
    }

    //删除品种类型
    @GetMapping("/deleteProductClass")
    @PermissionCheck("product_deleteProductClass")
    public SzResult deleteProductClass(@RequestParam String id, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return pdtClassBu.deleteProductClass(id, token, plantId);
    }

    //品种来源控制
    @GetMapping("/productClassfromCtrl")
    @PermissionCheck("product_productClassfromCtrl")
    public SzResult productClassfromCtrl(@RequestParam String id, @RequestParam String isOrNot, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return pdtClassBu.productClassfromCtrl(id, isOrNot, token, plantId);
    }

    /**
     * 品种授权查询接口
     *
     * @param co:公司名称 pdt:品种名称
     * @return SzResult
     */
    @GetMapping("/pdtAuth")
    public SzResult getPdtAuth(
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "pageSize", required = false) String pageSize,
            @RequestParam(value = "co", required = false) String co,
            @RequestParam(value = "chiCo", required = false) String chiCo,
            @RequestParam(value = "pdt", required = false) String pdt, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        if (co != null && co.contains(".")) {
                String[] split = co.split("\\.");
                co = split[0];
                chiCo = split[1];
        }

        return pdtAuthBu.getPdtAuth(page, pageSize, co, chiCo, pdt, token, plantId);
    }


    //新增产品
    @PostMapping("/addProduct")
    @PermissionCheck("product_addProduct")
    public SzResult addProduct(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String pdtInfClass = jsonObject.getString("pdtInfClass");//品种类型
        String pdtInfPdt = jsonObject.getString("pdtInfPdt");//品种名称
        Integer pdtInfPrice = jsonObject.getInteger("pdtInfPrice");//品种价格
        String affiliatedUnit = jsonObject.getString("affiliatedUnit");//所属单位
        String pdtInfSpeReadyList = jsonObject.getString("pdtInfSpeReadyList");//指定装载点预备车辆数列表
        String pdtInfSpePleaseOnCelist = jsonObject.getString("pdtInfSpePleaseOnCelist");//指定装载点一次请进的车辆数列表
        List<String> pdtinfSpeSitelist = jsonObject.getJSONArray("pdtinfSpeSitelist").toJavaList(String.class);//指定装载点序号列表
        String pdtinfSpeSnlist = jsonObject.getString("pdtinfSpeSnlist");//指定装载点品种编号列表
        String pdtinfSpeGateslist = jsonObject.getString("pdtinfSpeGateslist");//指定装载点料口数列表
        Integer pdtInfStockcls = jsonObject.getInteger("pdtInfStockcls");//库存分类
        Integer pdtinfSn = jsonObject.getInteger("pdtinfSn");//产品默认编号
        Integer pdtinfCapacity = jsonObject.getInteger("pdtinfCapacity");//产品品种库容量
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return productBu.addProduct(pdtInfClass, pdtInfPdt, pdtInfPrice, pdtinfSpeSitelist, pdtinfSpeSnlist, pdtinfSpeGateslist, affiliatedUnit, pdtInfSpeReadyList, pdtInfSpePleaseOnCelist, pdtInfStockcls, pdtinfSn,pdtinfCapacity, token, plantId);
    }

    //删除产品
    @GetMapping("/deleteProduct")
    @PermissionCheck("product_deleteProduct")
    public SzResult deleteProduct(@RequestParam String id, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return productBu.deleteProduct(id, token, plantId);
    }

    //修改产品
    @PostMapping("/updateProduct")
    @PermissionCheck("product_updateProduct")
    public SzResult updateProduct(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String pdtInfId = jsonObject.getString("pdtInfId");//品种id
        String pdtInfClass = jsonObject.getString("pdtInfClass");//品种类型
        String pdtInfPdt = jsonObject.getString("pdtInfPdt");//品种名称
        Integer pdtInfPrice = jsonObject.getInteger("pdtInfPrice");//品种价格
        String affiliatedUnit = jsonObject.getString("affiliatedUnit");//所属单位
        List<Integer> pdtInfSpeReadyList = jsonObject.getJSONArray("pdtInfSpeReadyList").toJavaList(Integer.class);//指定装载点预备车辆数列表
        List<Integer> pdtInfSpePleaseOnCelist = jsonObject.getJSONArray("pdtInfSpePleaseOnCelist").toJavaList(Integer.class);//指定装载点一次请进的车辆数列表
        List<String> pdtinfSpeSitelist =  jsonObject.getJSONArray("pdtinfSpeSitelist").toJavaList(String.class);//指定装载点序号列表
        List<Integer> pdtinfSpeSnlist = jsonObject.getJSONArray("pdtinfSpeSnlist").toJavaList(Integer.class);//指定装载点品种编号列表
        List<Integer> pdtinfSpeGateslist = jsonObject.getJSONArray("pdtinfSpeGateslist").toJavaList(Integer.class);//指定装载点料口数列表
        Integer pdtInfStockcls = jsonObject.getInteger("pdtInfStockcls");//库存分类
        Integer pdtinfSn = jsonObject.getInteger("pdtinfSn");//产品默认编号
        Integer pdtinfCapacity = jsonObject.getInteger("pdtinfCapacity");//产品品种库容量
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return productBu.updateProduct(pdtInfId, pdtInfClass, pdtInfPdt, pdtInfPrice, pdtinfSpeSitelist, pdtinfSpeSnlist, pdtinfSpeGateslist, affiliatedUnit, pdtInfSpeReadyList, pdtInfSpePleaseOnCelist, pdtInfStockcls, pdtinfSn,pdtinfCapacity, token, plantId);
    }


    //产品调价
    @PostMapping("/updateProductPrice")
    @PermissionCheck("product_updateProductPrice")
    public SzResult updateProductPrice(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        List<String> id = jsonObject.getJSONArray("id").toJavaList(String.class);//品种id
        String pdtInfPrice = jsonObject.getString("pdtInfPrice");//品种价格
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return pdtAuthBu.updatePdtPrice(id, pdtInfPrice, token, plantId);
    }



    //产品一键调价
    @PostMapping("/updatePdtPriceAll")
    @PermissionCheck("product_updatePdtPriceAll")
    public SzResult updatePdtPriceAll(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
//        String token,String price,String pdt
        String pdt = jsonObject.getString("pdt");//品种id
        String pdtInfPrice = jsonObject.getString("pdtInfPrice");//品种价格
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return pdtAuthBu.updatePdtPriceAll(token, pdtInfPrice, pdt, plantId);
    }

    /**
     * 导出excel
     *
     * @param jsonParam 传入的参数 filename 文件名  fields 实体类  ids 数据  isHeader 是否有表头
     * @param response  响应
     * @param request   请求
     * @return void 无返回值 IO流
     * @throws IOException          IO异常
     * @throws ExecutionException   异常
     * @throws InterruptedException 异常
     */
    @PostMapping("/exportExcel")
    public void exportExcel(@RequestBody JSONObject jsonParam, HttpServletResponse response, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
/*        QueryWrapper<SysUserPlantConn> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.eq("USER_ID", StpUtil.getLoginIdByToken(token).toString());
        SysUserPlantConn one2 = userPlantConnService.getOne(queryWrapper4);
        String plantId = one2.getPlantId();*/
        /*if (!userInfoDao.getUserPermissionWeb(StpUtil.getLoginIdByToken(token).toString()).containsKey("company_exportExcel")) {
            response.sendError(403,"您没有导出产品excel的权限");
        }*/
        List<Map<String, Object>> fields = jsonParam.getObject("fields", List.class);
        String filename = jsonParam.getString("filename");
        List<Map<String, Object>> ids = jsonParam.getObject("ids", List.class);
        boolean isHeader = jsonParam.getBoolean("isHeader");
        String mode = jsonParam.getString("mode");
        Boolean original = jsonParam.getBoolean("original");
        Map<String, String> pager = jsonParam.getObject("pager", Map.class);
        String sheetName = jsonParam.getString("sheetName");
        Map<String, String> form = jsonParam.getObject("form", Map.class);
        Future<List<PdtInfExcelEn>> listFuture = productBu.exportExcel(fields, filename, ids, isHeader, mode, original, pager, sheetName, form, plantId);
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        BorderStyle borderStyle = BorderStyle.THIN;
        short borderColor = IndexedColors.BLACK.getIndex();
        contentWriteCellStyle.setBorderTop(borderStyle);
        contentWriteCellStyle.setBorderBottom(borderStyle);
        contentWriteCellStyle.setBorderLeft(borderStyle);
        contentWriteCellStyle.setBorderRight(borderStyle);
        contentWriteCellStyle.setTopBorderColor(borderColor);
        contentWriteCellStyle.setBottomBorderColor(borderColor);
        contentWriteCellStyle.setLeftBorderColor(borderColor);
        contentWriteCellStyle.setRightBorderColor(borderColor);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(null, contentWriteCellStyle);
        List<PdtInfExcelEn> pdtInfExcelEn = listFuture.get();
        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PdtInfExcelEn.class)
                .registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("品种")
                .doWrite(pdtInfExcelEn);
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }


    @PostMapping("/pdtClassExportExcel")
    public void pdtClassExportExcel(@RequestBody JSONObject jsonParam, HttpServletResponse response, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
//        String plantId = null;
//        if (token != null) {
//            plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
//        }
        List<Map<String, Object>> fields = jsonParam.getObject("fields", List.class);
        String filename = jsonParam.getString("filename");
        List<Map<String, Object>> ids = jsonParam.getObject("ids", List.class);
        boolean isHeader = jsonParam.getBoolean("isHeader");
        String mode = jsonParam.getString("mode");
        Boolean original = jsonParam.getBoolean("original");
        Map<String, String> pager = jsonParam.getObject("pager", Map.class);
        String sheetName = jsonParam.getString("sheetName");
        Map<String, String> form = jsonParam.getObject("form", Map.class);
        String pdtClass = null;
        if (form.get("pdtClass") != null && !form.get("pdtClass").equals("null")) {
            pdtClass = form.get("pdtClass").toString();
        }
        Future<List<PdtClassExcelEn>> listFuture = productBu.pdtClassExportExcel(fields, filename, ids, isHeader, mode, original, pager, sheetName, plantId, pdtClass);
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        BorderStyle borderStyle = BorderStyle.THIN;
        short borderColor = IndexedColors.BLACK.getIndex();
        contentWriteCellStyle.setBorderTop(borderStyle);
        contentWriteCellStyle.setBorderBottom(borderStyle);
        contentWriteCellStyle.setBorderLeft(borderStyle);
        contentWriteCellStyle.setBorderRight(borderStyle);
        contentWriteCellStyle.setTopBorderColor(borderColor);
        contentWriteCellStyle.setBottomBorderColor(borderColor);
        contentWriteCellStyle.setLeftBorderColor(borderColor);
        contentWriteCellStyle.setRightBorderColor(borderColor);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(null, contentWriteCellStyle);
        List<PdtClassExcelEn> PdtClassExcelEn = listFuture.get();
        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PdtClassExcelEn.class)
                .registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("品种")
                .doWrite(PdtClassExcelEn);
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }

    /**
     * 品种授权导出excel
     *
     * @param jsonParam
     * @param response
     * @param request
     * @throws Exception
     */
    @PostMapping("/pdtAuthExportExcel")
    public void pdtAuthExportExcel(@RequestBody JSONObject jsonParam, HttpServletResponse response, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
//        String plantId = null;
//        if (token != null) {
//            plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
//        }
        List<Map<String, Object>> fields = jsonParam.getObject("fields", List.class);
        String filename = jsonParam.getString("filename");
        List<Map<String, Object>> ids = jsonParam.getObject("ids", List.class);
        boolean isHeader = jsonParam.getBoolean("isHeader");
        String mode = jsonParam.getString("mode");
        Boolean original = jsonParam.getBoolean("original");
        Map<String, String> pager = jsonParam.getObject("pager", Map.class);
        String sheetName = jsonParam.getString("sheetName");
        Map<String, String> form = jsonParam.getObject("form", Map.class);
        String pdtsauthCo = null;//授权单位
        String pdtsauthPdt = null;//授权品种
        if (form.get("pdtsauthCo") != null && !form.get("pdtsauthCo").equals("null")) {
            pdtsauthCo = form.get("pdtsauthCo").toString();
        }
        if (form.get("pdtsauthPdt") != null && !form.get("pdtsauthPdt").equals("null")) {
            pdtsauthPdt = form.get("pdtsauthPdt").toString();
        }
        Future<List<PdtAuthExcelEn>> listFuture = pdtAuthBu.pdtAuthExportExcel(fields, filename, ids, isHeader, mode, original, pager, sheetName, plantId, pdtsauthCo, pdtsauthPdt);
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        BorderStyle borderStyle = BorderStyle.THIN;
        short borderColor = IndexedColors.BLACK.getIndex();
        contentWriteCellStyle.setBorderTop(borderStyle);
        contentWriteCellStyle.setBorderBottom(borderStyle);
        contentWriteCellStyle.setBorderLeft(borderStyle);
        contentWriteCellStyle.setBorderRight(borderStyle);
        contentWriteCellStyle.setTopBorderColor(borderColor);
        contentWriteCellStyle.setBottomBorderColor(borderColor);
        contentWriteCellStyle.setLeftBorderColor(borderColor);
        contentWriteCellStyle.setRightBorderColor(borderColor);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(null, contentWriteCellStyle);
        List<PdtAuthExcelEn> pdtAuthExcelEn = listFuture.get();
        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PdtAuthExcelEn.class)
                .registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("品种授权")
                .doWrite(pdtAuthExcelEn);
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }


    //    新增品种授权
    @PostMapping("/addPdtAuth")
    @PermissionCheck("product_addPdtAuth")
    public SzResult addPdtAuth(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        String pdtsauthPdt = jsonObject.getString("pdtsauthPdt");//授权品种
        String pdtsauthPrice = jsonObject.getString("pdtsauthPrice");//授权价格
        String pdtsauthCo = jsonObject.getString("pdtsauthCo");//授权单位
        String pdtsauthChico = jsonObject.getString("pdtsauthChico");//授权单位子单位
        return pdtAuthBu.addPdtAuth(pdtsauthCo, pdtsauthChico, pdtsauthPdt, pdtsauthPrice, token, plantId);
    }

    //        冻结授权品种
    @PostMapping("/freezePdtAuth")
    @PermissionCheck("product_freezePdtAuth")
    public SzResult freezePdtAuth(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        String id = jsonObject.getString("id");//授权品种id
        Integer lock = jsonObject.getInteger("lock");//授权品种状态
        return pdtAuthBu.freezePdtAuth(id, lock, token, plantId);
    }

    //        删除授权品种
    @PostMapping("/deletePdtAuth")
    @PermissionCheck("product_deletePdtAuth")
    public SzResult deletePdtAuth(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        String id = jsonObject.getString("id");//授权品种id
        return pdtAuthBu.deletePdtAuth(id, token, plantId);
    }

    //        获取所有调价信息
    @GetMapping("/getPriceRecord")
    public SzResult getPriceRecord(@RequestParam(value = "page", required = false) Integer page,
                                   @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                   @RequestParam(value = "startTime", required = false) String startTime,
                                   @RequestParam(value = "endTime", required = false) String endTime,
                                   @RequestParam(value = "pdtId", required = false) String pdtId,
                                   @RequestParam(value = "cpyId", required = false) String cpyId,
                                   @RequestParam(value = "recordNo", required = false) BigDecimal recordNo,
                                   HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return pdtAuthBu.getPriceRecord(page, pageSize, startTime, endTime, recordNo, pdtId, cpyId, token, plantId);
    }

    //    修改调价信息的备注
    @PostMapping("/updatePriceRecord")
    @PermissionCheck("product_updatePriceRecord")
    public SzResult updatePriceRecord(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String id = jsonObject.getString("id");//调价记录id
        String remark = jsonObject.getString("remark");//调价记录备注
        String plantId = request.getHeader("Plant-Id");
        return pdtAuthBu.updatePriceRecord(id, remark, token, plantId);
    }


    @PostMapping("/priceRecordExportExcel")
    public void priceRecordExportExcel(@RequestBody JSONObject jsonParam, HttpServletResponse response, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        //        实体类
        List<Map<String, Object>> fields = jsonParam.getObject("fields", List.class);
//        文件名使用utf-8编码
        String filename = jsonParam.getString("filename");
//        数据
        List<Map<String, Object>> ids = jsonParam.getObject("ids", List.class);
//        是否有表头
        Boolean isHeader = jsonParam.getBoolean("isHeader");
//        mode为"current"时，导出当前页，为"all"时，导出所有页，selected时，导出选中数据
        String mode = jsonParam.getString("mode");
//        是否原始数据
        Boolean original = jsonParam.getBoolean("original");
//        分页
        Map<String, String> pager = jsonParam.getObject("pager", Map.class);
//        表格标题
        String sheetName = jsonParam.getString("sheetName");
        Map form = jsonParam.getObject("form", Map.class);
//        String truckNo, Integer truckType
        String coName = null;
        if (form.get("coName") != null && !form.get("coName").equals("null")) {
            coName = form.get("coName").toString();
        }
        String pdtName = null;
        if (form.get("pdtName") != null && !form.get("pdtName").equals("null")) {
            pdtName = form.get("pdtName").toString();
        }
        String recordNo = null;
        if (form.get("recordNo") != null && !form.get("recordNo").equals("null")) {
            recordNo = form.get("recordNo").toString();
        }
        String startTime = null;
        if (form.get("startTime") != null && !form.get("startTime").equals("null")) {
            startTime = form.get("startTime").toString();
        }
        String endTime = null;
        if (form.get("endTime") != null && !form.get("endTime").equals("null")) {
            endTime = form.get("endTime").toString();
        }
        Future<List<GetPriceRecordExcelEn>> listFuture = pdtAuthBu.priceRecordExportExcel(fields, filename, ids, isHeader, mode, original, pager, sheetName, coName, pdtName, recordNo, plantId, startTime, endTime);

        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        BorderStyle borderStyle = BorderStyle.THIN;
        short borderColor = IndexedColors.BLACK.getIndex();
        contentWriteCellStyle.setBorderTop(borderStyle);
        contentWriteCellStyle.setBorderBottom(borderStyle);
        contentWriteCellStyle.setBorderLeft(borderStyle);
        contentWriteCellStyle.setBorderRight(borderStyle);
        contentWriteCellStyle.setTopBorderColor(borderColor);
        contentWriteCellStyle.setBottomBorderColor(borderColor);
        contentWriteCellStyle.setLeftBorderColor(borderColor);
        contentWriteCellStyle.setRightBorderColor(borderColor);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(null, contentWriteCellStyle);

        List<GetPriceRecordExcelEn> getPriceRecordExcelEn = listFuture.get();
        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        if (filename == null || filename.equals("")) {
            filename = "调价记录";
        }
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xlsx");
        EasyExcel.write(response.getOutputStream(), GetPriceRecordExcelEn.class)
                .registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("调价记录")
                .doWrite(getPriceRecordExcelEn);
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }

    //    单品预调价1
    @PostMapping("/setPrePrice1")
    @PermissionCheck("edit_preset_price1")
    public SzResult setPrePrice(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String id = jsonObject.getString("id");//授权品种id
        String price = jsonObject.getString("PrePrice");//特价
        String startTime = jsonObject.getString("startTime");//特价开始时间
        String plantId = request.getHeader("Plant-Id");
        return specialOfferBu.setPrePrice1(id, price, startTime, token, plantId);
    }

    //    单品预调价2
    @PostMapping("/setPrePrice2")
    @PermissionCheck("edit_preset_price2")
    public SzResult setPrePrice2(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String id = jsonObject.getString("id");//授权品种id
        String price = jsonObject.getString("PrePrice");//优惠
        String startTime = jsonObject.getString("startTime");//优惠开始时间
        String plantId = request.getHeader("Plant-Id");
        return specialOfferBu.setPrePrice2(id, price, startTime, token,plantId);
    }
//    产品一键预调价1
//     setAllPrePrice3产品一键预调价1
//    public SzResult setAllPrePrice3( String pdtId,String price, String startTime, String endTime, String token, String plantId) throws Exception {
    @PostMapping("/setAllPrePrice1")
    @PermissionCheck("one_edit_preset_price1")
    public SzResult setAllPrePrice1(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String pdtId = jsonObject.getString("pdtId");//��品种id
        String price = jsonObject.getString("PrePrice");//特价
        String startTime = jsonObject.getString("startTime");//特价开始时间
        String plantId = request.getHeader("Plant-Id");
        return specialOfferBu.setAllPrePrice1(pdtId, price, startTime, token, plantId);
    }
    //    setAllPrePrice1产品一键预调价2
//    public SzResult setAllPrePrice2( String pdtId,String price, String startTime, String endTime, String token, String plantId) throws Exception {
    @PostMapping("/setAllPrePrice2")
    @PermissionCheck("one_edit_preset_price2")
    public SzResult setAllPrePrice2(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String pdtId = jsonObject.getString("pdtId");//��品种id
        String price = jsonObject.getString("PrePrice");//优��
        String startTime = jsonObject.getString("startTime");//优��开始时间
        String plantId = request.getHeader("Plant-Id");
        return specialOfferBu.setAllPrePrice2(pdtId, price, startTime, token, plantId);
    }
        //查所有出货类品种
    @GetMapping("/getOutProduct")
    public SzResult getOutProduct(HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return productBu.getOutProduct(token, plantId);
    }
//查所有石膏品种
    @GetMapping("/getGypsumProduct")
    public SzResult getGypsumProduct(
            @RequestParam(value = "pdtName", required = false) String pdtName,
            HttpServletRequest request) throws Exception {
        String plantId = request.getHeader("Plant-Id");
        return productBu.getGypsumProduct(pdtName, plantId);
    }
//    getGypsumAndZhaProduct查所有石膏、渣、湿渣品种为石膏渣计划做的
    @GetMapping("/getGypsumAndZhaProduct")
    public SzResult getGypsumAndZhaProduct(
            @RequestParam(value = "pdtName", required = false) String pdtName,
            HttpServletRequest request) throws Exception {
        String plantId = request.getHeader("Plant-Id");
        return productBu.getGypsumAndZhaProduct(pdtName, plantId);
    }
}

